package com.cskaoyan.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.mall.product.cache.RedisCache;
import com.cskaoyan.mall.product.converter.dto.SpuInfoConverter;
import com.cskaoyan.mall.product.dto.*;
import com.cskaoyan.mall.product.mapper.*;
import com.cskaoyan.mall.product.model.*;
import com.cskaoyan.mall.product.query.*;
import com.cskaoyan.mall.product.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpuServiceImpl implements SpuService{
    @Autowired
    SpuInfoMapper spuInfoMapper;
    @Autowired
    SpuInfoConverter spuInfoConverter;
    @Autowired
    SaleAttrInfoMapper saleAttrInfoMapper;
    @Autowired
    SpuSaleAttrInfoMapper spuSaleAttrInfoMapper;
    @Autowired
    SpuSaleAttrValueMapper spuSaleAttrValueMapper;
    @Autowired
    SpuImageMapper spuImageMapper;
    @Autowired
    SpuPosterMapper spuPosterMapper;
    @Autowired
    SkuSaleAttrValueMapper skuSaleAttrValueMapper;
    @Override
    public SpuInfoPageDTO getSpuInfoPage(Page<SpuInfo> pageParam, Long category3Id) {
        LambdaQueryWrapper<SpuInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpuInfo::getThirdLevelCategoryId,category3Id);
        Page<SpuInfo> spuInfoPage = spuInfoMapper.selectPage(pageParam, lambdaQueryWrapper);
        List<SpuInfoDTO> spuInfoDTOList = spuInfoConverter.spuInfoPO2DTOs(spuInfoPage.getRecords());
        return SpuInfoPageDTO.builder()
                .records(spuInfoDTOList)
                .total((int) spuInfoPage.getTotal ())
                .build();
    }

    @Override
    @Transactional
    public void saveSpuInfo(SpuInfoParam spuInfoParam) {
        //先添加spuInfo表的内容
        SpuInfo spuInfo=SpuInfo.builder()
                .spuName(spuInfoParam.getSpuName())
                .description(spuInfoParam.getDescription())
                .thirdLevelCategoryId(spuInfoParam.getCategory3Id())
                .tmId(spuInfoParam.getTmId())
                .build();
        spuInfoMapper.insert(spuInfo);
        //然后把图片内容添加了，很棒
        //先添加image
        for (SpuImageParam spuImageParam : spuInfoParam.getSpuImageList()) {
            spuImageMapper.insert(SpuImage.builder()
                            .spuId(spuInfo.getId())
                            .imgName(spuImageParam.getImgName())
                            .imgUrl(spuImageParam.getImgUrl())
                            .build());
        }

        //再添加海报
        for (SpuPosterParam spuPosterParam : spuInfoParam.getSpuPosterList()) {
            spuPosterMapper.insert(SpuPoster.builder()
                            .spuId(spuInfo.getId())
                            .imgName(spuPosterParam.getImgName())
                            .imgUrl(spuPosterParam.getImgUrl())
                            .build());
        }

        //加完图片加属性，真棒
        for (SpuSaleAttributeInfoParam infoParam : spuInfoParam.getSpuSaleAttrList()) {
            //先添加属性，再添加属性值
            SpuSaleAttributeInfo spuSaleAttributeInfo = SpuSaleAttributeInfo.builder()
                    .spuId(spuInfo.getId())
                    .saleAttrId(infoParam.getBaseSaleAttrId())
                    .saleAttrName(infoParam.getSaleAttrName())
                    .build();
            spuSaleAttrInfoMapper.insert(spuSaleAttributeInfo);
            for (SpuSaleAttributeValueParam valueParam : infoParam.getSpuSaleAttrValueList()) {
                spuSaleAttrValueMapper.insert(SpuSaleAttributeValue.builder()
                                .spuId(spuInfo.getId())
                                .spuSaleAttrId(spuSaleAttributeInfo.getId())
                                .spuSaleAttrValueName(valueParam.getSaleAttrValueName())
                                .build());
            }
        }

    }

    @Override
    public List<SpuImageDTO> getSpuImageList(Long spuId) {
        Map<String,Object> map=new HashMap<>();
        map.put("spu_id", spuId);
        List<SpuImage> spuImages = spuImageMapper.selectByMap(map);
        List<SpuImageDTO> spuImageDTOS = spuInfoConverter.spuImagePOs2DTOs(spuImages);
        return spuImageDTOS;
    }

    @Override
    public List<SpuSaleAttributeInfoDTO> getSpuSaleAttrList(Long spuId) {
        //根据spuId获取相关列表
        //获取相关属性和属性值
//        Map<String ,Object> infoValueMap=new HashMap<>();
//        infoValueMap.put("spu_id", spuId);
//        List<SpuSaleAttributeInfo> spuInfoList = spuSaleAttrInfoMapper.selectByMap(infoValueMap);
//        //然后设置属性值
//        for (SpuSaleAttributeInfo info : spuInfoList) {
//            Map<String,Object> valueMap=new HashMap<>();
//            valueMap.put("spu_sale_attr_id",info.getId());
//            List<SpuSaleAttributeValue> values = spuSaleAttrValueMapper.selectByMap(valueMap);
//            info.setSpuSaleAttrValueList(values);
//        }

        //这个地方要写spl写
        List<SpuSaleAttributeInfo> spuInfoList = spuSaleAttrInfoMapper.selectSpuSaleAttrList(spuId);

        //然后做个转换
        List<SpuSaleAttributeInfoDTO> infoDTOS = spuInfoConverter.spuSaleAttributeInfoPOs2DTOs(spuInfoList);

        return infoDTOS;
    }

    @Override
    @RedisCache(prefix = "product:findSpuPosterBySpuId")
    public List<SpuPosterDTO> findSpuPosterBySpuId(Long spuId) {
        //查找对应的post列表
        Map<String,Object> map=new HashMap<>();
        map.put("spu_id",spuId);
        List<SpuPoster> spuPosters = spuPosterMapper.selectByMap(map);

        return spuInfoConverter.spuPosterPOs2DTOs(spuPosters);
    }

    @Override
    @RedisCache(prefix = "product:getSkuValueIdsMap")
    public Map<String, Long> getSkuValueIdsMap(Long spuId) {
        //调map，然后返回
        List<SkuSaleAttributeValuePermutation> skuSaleAttributeValuePermutations = skuSaleAttrValueMapper.selectSaleAttrValuesBySpu(spuId);
        Map<String, Long> result = new HashMap<>();
        for (SkuSaleAttributeValuePermutation valuePermutation : skuSaleAttributeValuePermutations) {
            result.put(valuePermutation.getSkuSaleAttrValuePermutation(), valuePermutation.getSkuId());
        }

        return result;
    }

    @Override
    public List<SpuBaseSaleAttributeValueDTO> baseSaleAttrList() {
        List<SaleAttributeInfo> saleAttributeInfos = saleAttrInfoMapper.selectList(null);
        List<SpuBaseSaleAttributeValueDTO> spuBaseSaleAttributeValueDTOS = spuInfoConverter.baseSaleAttributePO2DTOs(saleAttributeInfos);

        return spuBaseSaleAttributeValueDTOS;
    }
}
